package com.youlu.campus.service.resume.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.UserResumeInfo;
import com.youlu.campus.service.resume.UserResumeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Slf4j
@Service
public class UserResumeInfoServiceImpl implements UserResumeInfoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserResumeInfo get(String userId) {
        log.info(":>>> 开始获取用户:{} 简历详情", userId);
        if (StringUtils.isBlank(userId)) {
            log.error(":>>> 活用用户简历错误:参数错误");
            throw new BusinessException("参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId));
        UserResumeInfo userResumeInfo = mongoTemplate.findOne(query, UserResumeInfo.class);
        return userResumeInfo;
    }

    @Override
    public boolean create(UserResumeInfo req) {
        log.info(":>>> 开始新建简历:{}", JSON.toJSON(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getUserId())) {
            log.error(":>>> 新建简历参数错误");
            throw new BusinessException("新建简历参数错误");
        }
        mongoTemplate.insert(req);
        return true;
    }

    @Override
    public boolean update(UserResumeInfo req) {
        log.info(":>>> 开始修改简历:{}", JSON.toJSON(req));
        if (Objects.isNull(req) || StringUtils.isBlank(req.getUserId()) || StringUtils.isBlank(req.getId())) {
            log.error(":>>> 修改简历参数错误");
            throw new BusinessException("修改简历参数错误");
        }
        mongoTemplate.save(req);
        return true;
    }
}
